import 'package:get/get.dart';
import 'package:my_app/app/data/model/user.dart';
import 'package:my_app/app/data/provider/api/dio_tool_api.dart';
import 'package:my_app/app/data/repository/dio_tool_repository.dart';

/// dio_tool_controller.dart
///
/// Created by fuchuanwei on Tue Feb 21 2023.
///
/// Description: Dio工具 Controller
///
class DioToolController extends GetxController {
  var selectedTypeOption = 0.obs;

  var responseStr = "".obs;

  final DioToolRepository _dioToolRepository = DioToolRepository(DioToolApi());

  var user = User().obs;

  @override
  void onInit() {
    super.onInit();
    fetchTestApi();
    fetchGetUser();
  }

  changeSelectedTypeOption(int value) =>
      {selectedTypeOption.value = value, fetchTestApi()};

  fetchTestApi() async {
    switch (selectedTypeOption.value) {
      case 0:
        responseStr.value = await _dioToolRepository.getTest();
        break;
      case 1:
        responseStr.value = await _dioToolRepository.postTest();
        break;
      case 2:
        responseStr.value = await _dioToolRepository.putTest();
        break;
      case 3:
        responseStr.value = await _dioToolRepository.deleteTest();
        break;
      default:
    }
  }

  fetchGetUser() async {
    user.value = await _dioToolRepository.getUser();
  }
}
